/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework.dal;

import framework.groups.GroupStrategy;
import framework.groups.Item;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 *
 * @author Guilherme
 */
public class ItemDB
{
    protected static String nameTableItem;
    
    public static void setNameTableItem(String s)
    {
        nameTableItem = s;
    }
    
    public static ArrayList<Item> getAll(Integer groupInteger)
    {
        ArrayList<Item> ns = new ArrayList<>();
        
        ResultSet rs = Link.select("itemGrupo", "*", "idGrupo = " + groupInteger);
        try
        {
            while (rs.next())
            {
                Integer noteInteger = new Integer(Integer.parseInt(rs.getString("idItem")));
                Item n = getItem(noteInteger);
                ns.add(n);
            }
        } catch (SQLException ex)
        {
            return null;
        }
        return ns;
    }
    
    public static ArrayList<Item> getAll()
    {
        ArrayList<Item> ns = new ArrayList<>();
        
        ResultSet rs = Link.select(nameTableItem, "*", "true");
        try
        {
            while (rs.next())
            {
                Integer noteInteger = new Integer(Integer.parseInt(rs.getString("id")));
                String description = rs.getString("descr");
                Timestamp salveDate = rs.getTimestamp("time");
                Item n = new Item(noteInteger, description);
                ns.add(n);
            }
        } catch (SQLException ex)
        {
            return null;
        }
        return ns;
    }
    
    public static Item getItem(Integer noteInteger)
    {
        ResultSet rs = Link.select(nameTableItem, "*", "id = " + noteInteger);
        Item n = null;
        
        try
        {
            while(rs.next())
            {
                String description = description = rs.getString("desc");
                n = new Item(noteInteger,description);
            }
        } catch (SQLException ex)
        {
            return n;
        }
        
        return n;
    }
    
    public static Integer getLastInteger()
    {
        ResultSet rs = Link.select(nameTableItem, "MAX(id)", "true");
        try
        {
            rs.next();
            return new Integer(rs.getInt("MAX(id)"));
            
        } catch (SQLException ex)
        {
            return null;
        }
    }
    
    public static boolean add(Item n)
    {
        return Link.add(nameTableItem, "(" + (getLastInteger() + 1) + ", '" + n.getDesc() +"')");
    }
    
    public static boolean add(Item n, int id)
    {
        return Link.add(nameTableItem, "(" + n.getIdentifier() + ", '" + n.getDesc() +"')") && 
                Link.add("itemgrupo", "(" + n.getIdentifier() + ", " + id + ")");
    }
    
    public static boolean remove(Item n)
    {
        return Link.delete(nameTableItem, "id = " + n.getIdentifier());
    }
    
    public static boolean update(Item n)
    {
        return Link.update(nameTableItem, "descr = '" + n.getDesc() + "'", "id = " + n.getIdentifier());
    }
}
